#include "Cloud.h"

Cloud::Cloud(Location l, int r, int c): size(r*c), loc(l), drawMe(true)
{
	Clouds cld;
	//cld = Clouds(225,0.2,s,s);
	cld = Clouds(253,0.001,r,c);

	vector<vector<int> > heightMap;
	heightMap.resize(r);
	for(int i=0;i<r;i++)
		heightMap[i].resize(c);

	for(int i=0;i<r;i++)
	{
		//float perlTotal=0;

		for(int j=0;j<c;j++)
		{
			//int stuff = cld.getExponHeightMap(i,j);
			heightMap[i][j] = cld.getExponHeightMap(i,j);
			int stuff = heightMap[i][j];
			float perlTotal=0;
		  /*
			if(stuff==254)
			{
				SceneNode *transNode = new TranslateNode(loc.getVal(0)+i,loc.getVal(1),loc.getVal(2)+j);
				GeomNode *cloudSphere = BuildSphere(2, 8, 8);
				cloudSphere->SetMaterial(stuff,stuff,stuff);
				transNode->AddChild(cloudSphere);
				root.AddChild(transNode);
			}
			if(stuff<254 && stuff>100)
			{
				SceneNode *transNode = new TranslateNode(loc.getVal(0)+i,loc.getVal(1),loc.getVal(2)+j);
				GeomNode *cloudSphere = BuildSphere(1, 8, 8);
				cloudSphere->SetMaterial(stuff,stuff,stuff);
				transNode->AddChild(cloudSphere);
				root.AddChild(transNode);
			}*/
			//if(stuff>127)
			if(stuff==255)
			{
				SceneNode *transNode = new TranslateNode(loc.getVal(0)+i,loc.getVal(1),loc.getVal(2)+j);
				//2.0(final)
				GeomNode *cloudSphere = BuildSphere(16.0, 8, 8);
				//cloudSphere->SetMaterial(float(float(stuff)/255),float(float(stuff)/255),float(float(stuff)/255));
				cloudSphere->SetMaterial(255,255,255);
				transNode->AddChild(cloudSphere);
				root.AddChild(transNode);
			}
			if(stuff==254)
			{
				float perlAve;
				PerlinNoise perl;
				perl = PerlinNoise(0.8,2.0,8);
				SceneNode *transNode = new TranslateNode(loc.getVal(0)+i*100,loc.getVal(1)+((perl.perlNoise2D(i,j,false)+1)*127),loc.getVal(2)+j*100);
				// 0.75 or 1.0(final)
				GeomNode *cloudSphere = BuildSphere(96.0, 8, 8);
				//cloudSphere->SetMaterial(float(float(stuff)/255),float(float(stuff)/255),float(float(stuff)/255));
				
				//perlTotal+=perl.perlNoise2D(i,j,false);
				perlTotal+=abs(perl.perlNoise2D(i,j,false))+20.0;
				if(i==0 && j==0)
					perlAve=perlTotal;
				else
					perlAve = (perlTotal/(i+j));

				std::cout << "perl average is: ";
				std::cout << perlAve;
				std::cout << "\n";


				//cloudSphere->SetMaterial(abs(perlAve)+.5,abs(perlAve)+.5,abs(perlAve)+.5);
				//cloudSphere->SetMaterial(perlAve,perlAve,perlAve);
				cloudSphere->SetMaterial(
					(perlAve+(abs(perl.perlNoise2D(i,j,false))*1.5)),
					(perlAve+(abs(perl.perlNoise2D(i,j,false))*1.5)),
					(perlAve+(abs(perl.perlNoise2D(i,j,false))*1.5)));
				//cloudSphere->SetMaterial(abs(perl.perlNoise2D(i,j,false))+.5,abs(perl.perlNoise2D(i,j,false))+.5,abs(perl.perlNoise2D(i,j,false))+.5);
				//cloudSphere->SetMaterial(24,24,24);
				transNode->AddChild(cloudSphere);
				root.AddChild(transNode);
			}
			if(stuff==253)
			{
				SceneNode *transNode = new TranslateNode(loc.getVal(0)+i,loc.getVal(1),loc.getVal(2)+j);
				//0.5
				GeomNode *cloudSphere = BuildSphere(4.0, 8, 8);
				//cloudSphere->SetMaterial(float(float(stuff)/255),float(float(stuff)/255),float(float(stuff)/255));
				cloudSphere->SetMaterial(255,255,255);
				transNode->AddChild(cloudSphere);
				root.AddChild(transNode);
			}
			if(stuff==252)
			{
				SceneNode *transNode = new TranslateNode(loc.getVal(0)+i,loc.getVal(1),loc.getVal(2)+j);
				//0.25
				GeomNode *cloudSphere = BuildSphere(4.0, 8, 8);
				//cloudSphere->SetMaterial(float(float(stuff)/255),float(float(stuff)/255),float(float(stuff)/255));
				cloudSphere->SetMaterial(255,255,255);
				transNode->AddChild(cloudSphere);
				root.AddChild(transNode);
			}
			if(stuff<253&&stuff>50)
			{
				SceneNode *transNode = new TranslateNode(loc.getVal(0)+i,loc.getVal(1),loc.getVal(2)+j);
				//0.1
				GeomNode *cloudSphere = BuildSphere(4.0, 8, 8);
				//cloudSphere->SetMaterial(float(float(stuff)/255),float(float(stuff)/255),float(float(stuff)/255));
				cloudSphere->SetMaterial(255,255,255);
				transNode->AddChild(cloudSphere);
				root.AddChild(transNode);
			}
		}
	}

}

Cloud::~Cloud()
{

}

SceneRoot* Cloud::getDrawables()
{
	return &root;
}
void Cloud::update()
{
	return;
}
int Cloud::getSize(void)
{
	return size;
}
Location Cloud::getLocation(void)
{
	return loc;
}
bool Cloud::draw()
{
	return drawMe;
}
bool Cloud::canSplit()
{
	return false;
}
std::list<WorldObject *> Cloud::split()
{
	return std::list<WorldObject *>();
}
void Cloud::setDraw(bool d)
{
	drawMe = d;
}

bool Cloud::deleteMe(void)
{
	return deleteThis;
}
void Cloud::setDelete(bool d)
{
	deleteThis = d;
}

/*void Cloud::specialAlgorithm(vector<vector<int> > heightMap,int s)
{
	for(int i=0;i<s;i++)
	{
		for(int j=0;j<s;j++)
		{
			if (heightMap[i][j]>75)
				specialAlgHelper(heightMap,s,i,j);
		}
	}
}

void specialAlgHelper(vector<vector<int> > heightMap, int s, int x, int y)
{

}*/